# =============================================================================
# Copyright (C) 2010 Diego Duclos
#
# This file is part of pyfa.
#
# pyfa is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pyfa is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pyfa.  If not, see <http://www.gnu.org/licenses/>.
# =============================================================================

# noinspection PyPackageRequirements
import wx

import config
from service.character import Character
from service.fit import Fit
import gui.graphFrame
import gui.globalEvents as GE
from gui.bitmap_loader import BitmapLoader

from logbook import Logger
# from service.crest import Crest
# from service.crest import CrestModes

pyfalog = Logger(__name__)


class MainMenuBar(wx.MenuBar):
    def __init__(self, mainFrame):
        pyfalog.debug("Initialize MainMenuBar")
        self.characterEditorId = wx.NewId()
        self.damagePatternEditorId = wx.NewId()
        self.targetResistsEditorId = wx.NewId()
        self.implantSetEditorId = wx.NewId()
        self.graphFrameId = wx.NewId()
        self.backupFitsId = wx.NewId()
        self.exportSkillsNeededId = wx.NewId()
        self.importCharacterId = wx.NewId()
        self.exportHtmlId = wx.NewId()
        self.wikiId = wx.NewId()
        self.forumId = wx.NewId()
        self.saveCharId = wx.NewId()
        self.saveCharAsId = wx.NewId()
        self.revertCharId = wx.NewId()
        self.eveFittingsId = wx.NewId()
        self.exportToEveId = wx.NewId()
        self.ssoLoginId = wx.NewId()
        self.attrEditorId = wx.NewId()
        self.toggleOverridesId = wx.NewId()
        self.importDatabaseDefaultsId = wx.NewId()
        self.toggleIgnoreRestrictionID = wx.NewId()
        self.devToolsId = wx.NewId()

        # pheonix: evaluate if this is needed
        if 'wxMac' in wx.PlatformInfo and wx.VERSION >= (3, 0):
            wx.ID_COPY = wx.NewId()
            wx.ID_PASTE = wx.NewId()

        self.mainFrame = mainFrame
        wx.MenuBar.__init__(self)

        # File menu
        fileMenu = wx.Menu()
        self.Append(fileMenu, "&文件")

        fileMenu.Append(self.mainFrame.addPageId, "&新的标签\tCTRL+T", "打开一个新的配置项")
        fileMenu.Append(self.mainFrame.closePageId, "&关闭标签\tCTRL+W", "关闭一个配置项")
        fileMenu.AppendSeparator()

        fileMenu.Append(self.backupFitsId, "&备份所有的配置", "备份所有配置到xml文件")
        fileMenu.Append(wx.ID_OPEN, "&导入配置\tCTRL+O", "导入配置到pyfa")
        fileMenu.Append(wx.ID_SAVEAS, "&导出配置\tCTRL+S", "导出配置为另一种格式")
        fileMenu.AppendSeparator()
        fileMenu.Append(self.exportHtmlId, "导出为HTML", "导出配置到HTML文件 (set in Preferences)")
        fileMenu.Append(self.exportSkillsNeededId, "导出技能需求", "为这个配置导出技能需求")
        fileMenu.Append(self.importCharacterId, "导入角色文件", "从文件导入角色")
        fileMenu.AppendSeparator()
        fileMenu.Append(wx.ID_EXIT,"退出")

        # Edit menu
        editMenu = wx.Menu()
        self.Append(editMenu, "&编辑")

        # editMenu.Append(wx.ID_UNDO)
        # editMenu.Append(wx.ID_REDO)

        editMenu.Append(wx.ID_COPY, "复制\tCTRL+C", "导出配置到剪贴板")
        editMenu.Append(wx.ID_PASTE, "粘贴\tCTRL+V", "从剪贴板导入一个配置")
        editMenu.AppendSeparator()
        editMenu.Append(self.saveCharId, "保存角色")
        editMenu.Append(self.saveCharAsId, "另存为角色")
        editMenu.Append(self.revertCharId, "恢复角色")
        editMenu.AppendSeparator()
        self.ignoreRestrictionItem = editMenu.Append(self.toggleIgnoreRestrictionID, "忽略配置约束")

        # Character menu
        windowMenu = wx.Menu()
        self.Append(windowMenu, "&窗口")

        charEditItem = wx.MenuItem(windowMenu, self.characterEditorId, "&角色编辑\tCTRL+E")
        charEditItem.SetBitmap(BitmapLoader.getBitmap("character_small", "gui"))
        windowMenu.Append(charEditItem)

        damagePatternEditItem = wx.MenuItem(windowMenu, self.damagePatternEditorId, "伤害模式编辑\tCTRL+D")
        damagePatternEditItem.SetBitmap(BitmapLoader.getBitmap("damagePattern_small", "gui"))
        windowMenu.Append(damagePatternEditItem)

        targetResistsEditItem = wx.MenuItem(windowMenu, self.targetResistsEditorId, "目标抗性编辑\tCTRL+R")
        targetResistsEditItem.SetBitmap(BitmapLoader.getBitmap("explosive_small", "gui"))
        windowMenu.Append(targetResistsEditItem)

        implantSetEditItem = wx.MenuItem(windowMenu, self.implantSetEditorId, "植入体设置编辑\tCTRL+I")
        implantSetEditItem.SetBitmap(BitmapLoader.getBitmap("hardwire_small", "gui"))
        windowMenu.Append(implantSetEditItem)

        graphFrameItem = wx.MenuItem(windowMenu, self.graphFrameId, "图表\tCTRL+G")
        graphFrameItem.SetBitmap(BitmapLoader.getBitmap("graphs_small", "gui"))
        windowMenu.Append(graphFrameItem)

        if not gui.graphFrame.graphFrame_enabled:
            self.Enable(self.graphFrameId, False)

        preferencesShortCut = "CTRL+," if 'wxMac' in wx.PlatformInfo else "CTRL+P"
        preferencesItem = wx.MenuItem(windowMenu, wx.ID_PREFERENCES, "偏好设置\t" + preferencesShortCut)
        preferencesItem.SetBitmap(BitmapLoader.getBitmap("preferences_small", "gui"))
        windowMenu.Append(preferencesItem)

        # self.sEsi = Crest.getInstance()

        # CREST Menu
        esiMMenu = wx.Menu()
        self.Append(esiMMenu, "EVE &SSO")

        esiMMenu.Append(self.ssoLoginId, "管理角色")
        esiMMenu.Append(self.eveFittingsId, "浏览EVE配置")
        esiMMenu.Append(self.exportToEveId, "导出到EVE")

        # if self.sEsi.settings.get('mode') == CrestModes.IMPLICIT or len(self.sEsi.getCrestCharacters()) == 0:
        self.Enable(self.eveFittingsId, True)
        self.Enable(self.exportToEveId, True)

        if not self.mainFrame.disableOverrideEditor:
            windowMenu.AppendSeparator()
            attrItem = wx.MenuItem(windowMenu, self.attrEditorId, "属性超载\tCTRL+B")
            attrItem.SetBitmap(BitmapLoader.getBitmap("fit_rename_small", "gui"))
            windowMenu.Append(attrItem)
            windowMenu.Append(self.toggleOverridesId, "超载开关")

        # Help menu
        helpMenu = wx.Menu()
        self.Append(helpMenu, "&帮助")
        helpMenu.Append(self.wikiId, "Wiki", "EVE WIKI")
        helpMenu.Append(self.forumId, "论坛", "EVE论坛")
        helpMenu.AppendSeparator()
        helpMenu.Append(self.importDatabaseDefaultsId, "导入默认数据库", "导入丢失的默认数据库")
        helpMenu.AppendSeparator()
        helpMenu.Append(wx.ID_ABOUT,"关于")

        if config.debug:
            helpMenu.Append(self.mainFrame.widgetInspectMenuID, "Open Widgets Inspect tool",
                            "Open Widgets Inspect tool")
            helpMenu.Append(self.devToolsId, "Open Dev Tools", "Dev Tools")

        self.mainFrame.Bind(GE.FIT_CHANGED, self.fitChanged)

    def fitChanged(self, event):
        enable = event.fitID is not None
        self.Enable(wx.ID_SAVEAS, enable)
        self.Enable(wx.ID_COPY, enable)
        self.Enable(self.exportSkillsNeededId, enable)

        sChar = Character.getInstance()
        charID = self.mainFrame.charSelection.getActiveCharacter()
        char = sChar.getCharacter(charID)

        # enable/disable character saving stuff
        self.Enable(self.saveCharId, not char.ro and char.isDirty)
        self.Enable(self.saveCharAsId, char.isDirty)
        self.Enable(self.revertCharId, char.isDirty)

        self.Enable(self.toggleIgnoreRestrictionID, enable)

        if event.fitID:
            sFit = Fit.getInstance()
            fit = sFit.getFit(event.fitID)

            if fit.ignoreRestrictions:
                self.ignoreRestrictionItem.SetItemLabel("使能配置约束")
            else:
                self.ignoreRestrictionItem.SetItemLabel("禁止配置约束")

        event.Skip()
